package com.reggie.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.common.ResultResponseVo;
import com.reggie.common.enums.front.AddressBookEnum;
import com.reggie.module.entity.po.AddressBook;
import com.reggie.module.mapper.AddressBookMapper;
import com.reggie.module.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

import java.time.LocalDateTime;
import java.util.List;

import static com.reggie.common.Constant.USER_SESSION;

/**
 * @Author 早睡蛋
 * @Date 2022/9/28 15:23:47
 * @Desc:
 */
@Service
@Slf4j
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;


    /**
     * 新增地址
     * @param addressBook
     * @param request
     * @return
     */
    @Override
    public ResultResponseVo saveAddress(AddressBook addressBook, HttpServletRequest request) {
        Long userId = (Long)request.getSession().getAttribute(USER_SESSION);
        addressBook.setUserId(userId);
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBook.setCreateUser(userId);
        addressBook.setUpdateUser(userId);
        int insert = addressBookMapper.insert(addressBook);
        if (insert>0){
            return AddressBookEnum.ADD_OK.getResultResponseVo();
        }
        return AddressBookEnum.ADD_FAIL.getResultResponseVo();
    }

    /**
     * 根据用户id获取用户的收货地址
     * @param request
     * @return
     */
    @Override
    public ResultResponseVo getListByUserId(HttpServletRequest request) {
        Long userId = (Long)request.getSession().getAttribute(USER_SESSION);
        QueryWrapper<AddressBook> addressBookQueryWrapper = new QueryWrapper<>();
        addressBookQueryWrapper
                .select("id","detail","label","user_id","consignee","phone","sex","is_default")
                .eq("user_id",userId)
                .last("order by is_default desc,update_time desc");
        List<AddressBook> addressBookList = addressBookMapper.selectList(addressBookQueryWrapper);
        return AddressBookEnum.SUCCESS.getResultResponseVo().setData(addressBookList);
    }

    /**
     * 设置默收货地址
     * @param id
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResultResponseVo setDefault(Long id, HttpServletRequest request) {
        Long userId = (Long)request.getSession().getAttribute(USER_SESSION);
        QueryWrapper<AddressBook> addressBookQueryWrapper = new QueryWrapper<>();
        addressBookQueryWrapper.eq("user_id",userId);
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(0);
        int update = addressBookMapper.update(addressBook, addressBookQueryWrapper);
        addressBook.setId(id);
        addressBook.setIsDefault(1);
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBook.setUpdateUser(userId);
        int i = addressBookMapper.updateById(addressBook);
        if (update>0 && i>0){
            return AddressBookEnum.SET_DEFAULT_OK.getResultResponseVo();
        }
        return AddressBookEnum.SET_DEFAULT_FAIL.getResultResponseVo();
    }

    /**
     * 根据id查询出收货地址信息
     * @param id
     * @return
     */
    @Override
    public ResultResponseVo getAddressById(Long id) {
        AddressBook addressBook = addressBookMapper.selectById(id);
        return AddressBookEnum.SUCCESS.getResultResponseVo().setData(addressBook);
    }

    /**
     * 更新地址信息
     * @param addressBook
     * @param request
     * @return
     */
    @Override
    public ResultResponseVo updateAddress(AddressBook addressBook, HttpServletRequest request) {
        Long userId = (Long)request.getSession().getAttribute(USER_SESSION);
        addressBook.setUpdateUser(userId);
        addressBook.setUpdateTime(LocalDateTime.now());
        int updateById = addressBookMapper.updateById(addressBook);
        if (updateById>0){
            return AddressBookEnum.UPDATE_OK.getResultResponseVo();
        }
        return AddressBookEnum.UPDATE_FAIL.getResultResponseVo();
    }

    /**
     * 查询默认地址
     * @param request
     * @return
     */
    @Override
    public ResultResponseVo getDefaultAddress(HttpServletRequest request) {
        Long userId = (Long)request.getSession().getAttribute(USER_SESSION);
        QueryWrapper<AddressBook> addressBookQueryWrapper = new QueryWrapper<>();
        addressBookQueryWrapper
                .eq("user_id",userId)
                .eq("is_default",1);
        AddressBook addressBook = addressBookMapper.selectOne(addressBookQueryWrapper);
        return AddressBookEnum.SUCCESS.getResultResponseVo().setData(addressBook);
    }
}
